//Bhaskar added this function on 6 December 2003
function checkLotNumber()
{
	var p1 = -1;
	var p2 = -1;
	var lotName = '';
	var flag = true;
	for (var count =0; count < document.sortDataEntry.lotId.length; count++ )
	{
		p1 = document.sortDataEntry.lotId[count].value.indexOf('$')+1;
		p2 = document.sortDataEntry.lotId[count].value.lastIndexOf('$');
		lotName = document.sortDataEntry.lotId[count].value.substring(p1, p2).trim();
		if (lotName.toUpperCase() == document.sortDataEntry.lotNo.value.trim().toUpperCase())
		{
			flag = false;
			break;
		}
	}
	return flag;
}

function locationSelected(form)
{
    var pa=document.getElementById("message");
    var x = form.locationId;

    for(var i=0; i< x.length; i++)
    {
        if(x.options[i].selected)
        {
	        if(x.options[i].value == "0")
            {
	            pa.innerHTML = "<li>Select Location";
                x.focus();
	            return false;
            }
        }
    }
    return true;
}

function productSelected(form){

    var pa=document.getElementById("message");
    var x = form.processStepId;

    for(var i=0; i< x.length; i++)
    {
        if(x.options[i].selected)
        {
	        if(x.options[i].value == "0")
            {
	            pa.innerHTML = "<li>Select Product Name";
                x.focus();
	            return false;
            }
        }
    }
    return true;
}

function lotNoSelected(form){

    var pa=document.getElementById("message");
    var x = form.lotId;

    for(var i=0; i< x.length; i++)
    {
        if(x.options[i].selected)
        {
	        if(x.options[i].value == "0")
            {
	            pa.innerHTML = "<li>Select Lot Number";
                x.focus();
	            return false;
            }
        }
    }
    return true;
}

//Bhaskar on 21 Feb 2004
function checkYieldDateOut()
{
	var bool = false;
	var form = document.sortDataEntry;
	var errors = ""
	var pa = document.getElementById ("message");
	pa.innerHTML = errors;
	if ( ( form.quantityOut.value.trim().length > 0 ) &&  ( form.testDateOut.value.trim().length > 0 ) )
	{
		if ( parseInt ( form.yield.value.trim().length ) == 0 && ( parseInt ( form.grossDice.value.trim().length ) == 0 ) )
		{
			form.quantityOut.disabled = false;
			form.yield.disabled = true;
			pa.innerHTML = 'Please enter a value for Gross Die'
			//form.grossDice.focus();
			bool = false;
		}
		else if ( parseInt ( form.yield.value.trim().length ) == 0 ) 
		{
			
			if ( checkInteger ( form.grossDice.value, ' Gross Die ', 'Y' ) == false )
			{
				form.quantityOut.disabled = false;
				form.yield.disabled = true;
				pa.innerHTML = errMsg;
				//form.grossDice.focus();
				bool = false;
			}
			else if ( parseInt ( form.grossDice.value ) < 1 )
			{
				form.quantityOut.disabled = false;
				form.yield.disabled = true;
				pa.innerHTML = 'Gross Die value cannot be zero';
				//form.grossDice.focus();
				bool = false;
			}
			else
			{
				var avgGoodDie = formatYieldIntoDoubleDecimalMode ( parseInt ( form.quantityOut.value )/ ( parseInt (form.waferQuantity.value) ) );
				var tempYield = parseInt ( form.quantityOut.value )/ (parseInt ( form.grossDice.value ) * ( parseInt (form.waferQuantity.value) ) ) * 100
				form.yield.value = formatYieldIntoDoubleDecimalMode ( tempYield );
				form.avgGoodDie.value = avgGoodDie;
				bool = true;
			}
		}
		else
		{
			if ( checkInteger ( form.grossDice.value, ' Gross Die ', 'Y' ) == false )
			{
				form.quantityOut.disabled = false;
				form.yield.disabled = true;
				pa.innerHTML = errMsg;
				//form.grossDice.focus();
				bool = false;
			}
			else if ( parseInt ( form.grossDice.value ) < 1 )
			{
				form.quantityOut.disabled = false;
				form.yield.disabled = true;
				pa.innerHTML = 'Gross Die value cannot be zero';
				//form.grossDice.focus();
				bool = false;
			}
			else
			{
				var avgGoodDie = formatYieldIntoDoubleDecimalMode ( parseInt ( form.quantityOut.value )/ ( parseInt (form.waferQuantity.value) ) );
				var tempYield = parseInt ( form.quantityOut.value )/ (parseInt ( form.grossDice.value ) * ( parseInt (form.waferQuantity.value) ) ) * 100
				form.yield.value = formatYieldIntoDoubleDecimalMode ( tempYield );
				form.avgGoodDie.value = avgGoodDie;
				bool = true;
			}
		}
	}

	else if ( ( form.quantityOut.value.trim().length > 0 ) &&  ( parseInt (form.testDateOut.value.trim().length ) == 0 ) )
	{
		if ( parseInt ( form.yield.value.trim().length ) == 0 && ( parseInt ( form.grossDice.value.trim().length ) == 0 ) )
		{
			form.quantityOut.disabled = false;
			form.yield.disabled = true;
			pa.innerHTML = 'Please enter a value for Gross Die'
			//form.grossDice.focus();
			bool = false;
		}
		else if ( parseInt ( form.yield.value.trim().length ) == 0 ) 
		{
			if ( checkInteger ( form.grossDice.value, ' Gross Die ', 'Y' ) == false )
			{
				form.quantityOut.disabled = false;
				form.yield.disabled = true;
				pa.innerHTML = errMsg;
				//form.grossDice.focus();
				bool = false;
			}
			else if ( parseInt ( form.grossDice.value ) < 1 )
			{
				form.quantityOut.disabled = false;
				form.yield.disabled = true;
				pa.innerHTML = 'Gross Die value cannot be zero';
				//form.grossDice.focus();
				bool = false;
			}
			else
			{
				var avgGoodDie = formatYieldIntoDoubleDecimalMode ( parseInt ( form.quantityOut.value )/ ( parseInt (form.waferQuantity.value) ) );
				var tempYield = parseInt ( form.quantityOut.value )/ (parseInt ( form.grossDice.value ) * ( parseInt (form.waferQuantity.value) ) ) * 100
				form.yield.value = formatYieldIntoDoubleDecimalMode ( tempYield );
				form.avgGoodDie.value = avgGoodDie;
				bool = true;
			}
		}
		else
		{
			if ( checkInteger ( form.grossDice.value, ' Gross Die ', 'Y' ) == false )
			{
				form.quantityOut.disabled = false;
				form.yield.disabled = true;
				pa.innerHTML = errMsg;
				//form.grossDice.focus();
				bool = false;
			}
			else if ( parseInt ( form.grossDice.value ) < 1 )
			{
				form.quantityOut.disabled = false;
				form.yield.disabled = true;
				pa.innerHTML = 'Gross Die value cannot be zero';
				//form.grossDice.focus();
				bool = false;
			}
			else
			{
				var avgGoodDie = formatYieldIntoDoubleDecimalMode ( parseInt ( form.quantityOut.value )/ ( parseInt (form.waferQuantity.value) ) );
				var tempYield = parseInt ( form.quantityOut.value )/ (parseInt ( form.grossDice.value ) * ( parseInt (form.waferQuantity.value) ) ) * 100
				form.yield.value = formatYieldIntoDoubleDecimalMode ( tempYield );
				form.avgGoodDie.value = avgGoodDie;
				bool = true;
			}
		}
	}
	
	
	else
	{
		bool = true;
	}
	return bool;
}

function formatYieldIntoDoubleDecimalMode ( yieldValu )
{
	var yldValue = yieldValu;
	yldValue = ''+(Math.round(yieldValu*100))/100;
	var p1 = yldValue.indexOf('.');
	if ( parseInt ( p1 ) == -1 )
	{
		yldValue += '.00'
	}
	else if ( parseInt ( p1 ) != -1 )
	{
		var p2 = yldValue.substring ( p1, yldValue.length );
		if ( parseInt( p2.length ) < 3 )
		{
			yldValue += '0';
		}
	}
	return yldValue;
}


function validateDieSize ()
{
	var bool = true;
	var pa=document.getElementById("message");
	pa.innerHTML = "";

	document.sortDataEntry.dieSize.value = document.sortDataEntry.dieSize.value.trim();
	if ( document.sortDataEntry.dieSize.value.length > 0 )
	{
		if ( isNaN ( document.sortDataEntry.dieSize.value ) )
		{
			document.sortDataEntry.dieSize.focus();
			pa.innerHTML = "<li> Please enter a valid Number for Die Size";
			bool = false;
		}
	}

	if ( document.sortDataEntry.hiddenDieSize.value.trim().length > 0 )
	{
		if ( parseInt ( document.sortDataEntry.dieSize.value ) != parseInt ( document.sortDataEntry.hiddenDieSize.value ) )
		{
			pa.innerHTML = "<li> Die size is changed, reenter bin data";
			document.sortDataEntry.dieSize.focus();
			bool = false;
		}
	}
	return bool;
}


function validateWaferQuantity ()
{
	var bool = true;
	var pa=document.getElementById("message");
	pa.innerHTML = "";

	document.sortDataEntry.waferQuantity.value = document.sortDataEntry.waferQuantity.value.trim();
	if(document.sortDataEntry.waferQuantity.value.trim()=='')
    {
        document.sortDataEntry.waferQuantity.focus();
        pa.innerHTML = "<li> Please Enter Wafer Quantity";
		bool = false;
		return bool;
    }
    if( document.sortDataEntry.waferQuantity.value.trim() == 0 )
    {
        document.sortDataEntry.waferQuantity.focus();
        pa.innerHTML = "<li>Zero value is not permitted for Wafer Quantity, Please enter greater than Zero";
		bool = false;
		return bool;
    }

	if ( document.sortDataEntry.waferQuantity.value.length > 0 )
	{
		if ( checkInteger( document.sortDataEntry.waferQuantity.value , ' Wafer Quantity ' , 'Y'  ) == false)
		{
			pa.innerHTML  = "<li> " + errMsg;
			document.sortDataEntry.waferQuantity.focus();
			bool = false;
			return bool;
		}
	}
	
	if ( document.sortDataEntry.hiddenWaferQuantity.value.trim().length > 0 )
	{
		if ( parseInt ( document.sortDataEntry.waferQuantity.value ) != parseInt ( document.sortDataEntry.hiddenWaferQuantity.value ) )
		{
			pa.innerHTML = "<li> Wafer Quantity is changed, reenter bin data";
			document.sortDataEntry.waferQuantity.focus();
			bool = false;
			return bool;
		}
	}

	return bool;
}


function validateGrossDice ()
{
	var bool = true;
	var pa=document.getElementById("message");
	pa.innerHTML = "";

	if ( (document.sortDataEntry.grossDice.value).trim().length  > 0 )
	{
		if ( checkInteger ( document.sortDataEntry.grossDice.value,' Gross Die ','Y'  ) == false )
		{
			pa.innerHTML = errMsg;
			document.sortDataEntry.grossDice.focus();
			bool = false;
		}
	}
	return bool;

}


function validateLotId ()
{
	var bool = true;
	var pa=document.getElementById("message");
	pa.innerHTML = "";

    if(document.sortDataEntry.lotId.value =="-1")
    {
        if(document.sortDataEntry.lotNo.value=='')
        {
            pa.innerHTML = "Please Enter Lot Number";
            document.sortDataEntry.lotNo.focus();
			bool = false;
            return bool;
        }
        if( document.sortDataEntry.lotNo.value.indexOf("'") != "-1" )
        {
            pa.innerHTML = "<li>Single quotation is not acceptable in Lot Number</li>";
            document.sortDataEntry.lotNo.focus();
            bool = false;
            return bool;
        }
		if ( !checkLotNumber() )
		{
			pa.innerHTML = 'Please enter a different Lot number. This lot number already exists';
			document.sortDataEntry.lotNo.focus();
			bool = false;
			return bool;
		}
	}
	return bool;
}